package main

import (
	"fmt"
	"sync"
	"time"
)

var (
	sum     int
	RWMutex sync.RWMutex
)

func main() {
	for i := 0; i < 100; i++ {
		go add(10)
	}

	for i := 0; i < 10; i++ {
		go fmt.Println("和为:", readSum())
	}
	time.Sleep(2 * time.Second)
}
func add(n int) {
	RWMutex.Lock()
	defer RWMutex.Unlock()
	sum += n
}
func readSum() int {
	RWMutex.RLock()
	defer RWMutex.RUnlock()
	b := sum
	return b
}
